package com.alumni_circle.core.utils;

import cn.hutool.core.util.StrUtil;
import com.alumni_circle.constant.PathConstant;

import java.io.File;

/**
 * @author 龙小虬
 * @since 2020-07-18 12:21
 */
public class Modify {

    /**
     * 将临时图片转为需要的图片
     * @param image
     * @return
     */
    public static String modifyImage(String image){
        if(image==null || ("").equals(image)){
            return null;
        }
        String filePath= PathConstant.SERVER.getPath();
//        String filePath="/Users/longdaye/Documents/longbin/长医校友圈/alumni_circle_ssm/alumni_circle/img";
        File file = new File(filePath);
        String newName;
        String imagesPath="";
        //传过来的文件以","分割
        String[] imageNames = image.split(",");
        for (int i = 0; i < imageNames.length; i++) {
            newName = imageNames[i].replace("{-temporary}",""+i);
            new File(filePath+"/"+imageNames[i]).renameTo(new File(filePath+"/"+newName));
            imagesPath += newName+";";
        }
        return imagesPath;
    }


    public static String modifyImageIndex(String image){
        if(StrUtil.hasEmpty(image)){
            return null;
        }
        String filePath=PathConstant.SERVER.getPath();
//        String filePath="/Users/longdaye/Documents/longbin/长医校友圈/alumni_circle_ssm/alumni_circle/img";
        File file = new File(filePath);
        String newName;
        String imagesPath="";
        String imageNames="";
        //传过来的文件以","分割
        if(!image.contains(",")){
            imageNames = image;
        }else {
            imageNames = image.substring(0, image.indexOf(","));
        }
        newName = imageNames.replace("{-temporary}","0");
        new File(filePath+"/"+imageNames).renameTo(new File(filePath+"/"+newName));
        imagesPath = filePath+"/"+newName;
        return newName;
    }

    public static String modifyImageLast(String image){
        String filePath=PathConstant.SERVER.getPath();
//        String filePath="/Users/longdaye/Documents/longbin/长医校友圈/alumni_circle_ssm/alumni_circle/img";
        File file = new File(filePath);
        String newName;
        StringBuilder imagesPath= new StringBuilder();
        if(!image.contains(",")){
            return null;
        }else {
            image = image.substring(image.indexOf(",")+1);
        }
        //传过来的文件以","分割
        String[] imageNames = image.split(",");
        for (int i = 0; i < imageNames.length; i++) {
            newName = imageNames[i].replace("{-temporary}",""+i);
            new File(filePath+"/"+imageNames[i]).renameTo(new File(filePath+"/"+newName));
            if(imageNames.length-1 != i) {
//                imagesPath += filePath + "/" + newName + ";";
                imagesPath.append(newName).append(";");
            }else {
//                imagesPath += filePath + "/" + newName;
                imagesPath.append(newName);
            }
        }
        return imagesPath.toString();
    }
}
